package com.osmino.contacts.entity;

import android.content.Context;
import android.content.res.Resources;
import android.database.Cursor;
import android.provider.ContactsContract;
import com.osmino.contacts.R;

public final class Phone implements Comparable<Phone> {

	public static final String ID_COLUMN = ContactsContract.CommonDataKinds.Phone._ID;
	public static final String NUMBER_COLUMN = ContactsContract.CommonDataKinds.Phone.NUMBER;
	public static final String TYPE_COLUMN = ContactsContract.CommonDataKinds.Phone.TYPE;
	public static final String LABEL_COLUMN = ContactsContract.CommonDataKinds.Phone.LABEL;
	public static final String RAW_CONTACT_ID_COLUMN = ContactsContract.CommonDataKinds.Phone.RAW_CONTACT_ID;
	public final long id;
	public final String number;
	public final int type;
	public final String label;
	public final long rawContactId;

	public Phone(long id, String number, int type, String label, long rawContactId) {
		this.id = id;
		this.number = number;
		this.type = type;
		this.label = label;
		this.rawContactId = rawContactId;
	}

	public Phone(Context context, Cursor cursor) {
		id = cursor.getLong(cursor.getColumnIndex(ID_COLUMN));
		number = resolveNumber(cursor);
		type = resolveType(cursor);
		label = resolveLabel(context, cursor, type);
		rawContactId = resolveRawContactId(cursor);
	}

	private static String resolveNumber(Cursor cursor) {
		String result = "";
		int columnIndex = cursor.getColumnIndex(NUMBER_COLUMN);
		if (columnIndex != -1) {
			result = cursor.getString(columnIndex);
		}
		return result;
	}

	private static int resolveType(Cursor cursor) {
		int result = -1;
		int columnIndex = cursor.getColumnIndex(TYPE_COLUMN);
		if (columnIndex != -1) {
			result = cursor.getInt(columnIndex);
		}
		return result;
	}

	private static String resolveLabel(Context context, Cursor cursor, int type) {
		String result = null;
		if (type != -1) {
			String label = null;
			if (type == ContactsContract.CommonDataKinds.Phone.TYPE_CUSTOM) {
				int columnIndex = cursor.getColumnIndex(LABEL_COLUMN);
				if (columnIndex != -1) {
					label = cursor.getString(columnIndex);
				} else {
					label = null;
				}
			}
			Resources res = context.getResources();
			result = ContactsContract.CommonDataKinds.Phone.getTypeLabel(res, type, label)
					.toString();
		} else {
			result = context.getString(R.string.unknown);
		}
		return result;
	}
	
	private static long resolveRawContactId(Cursor cursor) {
		int columnIndex = cursor.getColumnIndex(RAW_CONTACT_ID_COLUMN);
		return cursor.getLong(columnIndex);
	}

	@Override
	public int compareTo(Phone another) {
		int result = Long.valueOf(id).compareTo(another.id);
		if (result == 0) {
			result = number.compareTo(another.number);
		}
		if (result == 0) {
			result = Integer.valueOf(type).compareTo(another.type);
		}
		if (result == 0) {
			result = label.compareTo(another.label);
		}
		if (result == 0) {
			result = Long.valueOf(rawContactId).compareTo(another.rawContactId);
		}
		return result;
	}

	@Override
	public boolean equals(Object o) {
		if (this == o) {
			return true;
		}
		if (!(o instanceof Phone)) {
			return false;
		}
		Phone rhs = (Phone) o;
		return compareTo(rhs) == 0;
	}

	@Override
	public int hashCode() {
		int result = 17;
		result = 31 * result + (int) (id ^ (id >>> 32));
		result = 31 * result + number.hashCode();
		result = 31 * result + type;
		result = 31 * result + label.hashCode();
		result = 31 * result + (int) (rawContactId^ (rawContactId >>> 32));
		return result;
	}
}
